package com.dong.c03_channel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <h1></h1>
 *
 * @author : dzj
 * @create: 2020-03-17 23:14
 */
public class P3_NioFileChannelCopy {
    public static void main(String[] args) throws IOException {
        String fileName = "d:\\file01.txt";
        String fileNameTarget = "d:\\file01_bak.txt";
        FileInputStream fis = new FileInputStream(fileName);
        FileChannel readChannel = fis.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(100);
        int len = 0;
        FileOutputStream fos = new FileOutputStream(fileNameTarget);
        FileChannel writeChannel = fos.getChannel();
        while((len=readChannel.read(buffer))!=-1) {
            readChannel.read(buffer);
            buffer.flip();
            writeChannel.write(buffer);
            buffer.clear();
        }
        readChannel.close();
        writeChannel.close();;
        fis.close();
        fos.close();


    }
}
